package com.dxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dxp.common.BaseContext;
import com.dxp.common.R;
import com.dxp.pojo.AddressBook;
import com.dxp.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    //展示地址薄
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        addressBookQueryWrapper.eq(addressBook.getUserId() != null, "user_id", addressBook.getUserId());
        addressBookQueryWrapper.orderByDesc("update_time");
        List<AddressBook> list = addressBookService.list(addressBookQueryWrapper);
        return R.success(list);
    }
    //新增地址
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新增成功");
    }
    //设为默认地址
    @PutMapping("/default")
    public R<String> setdefault(@RequestBody AddressBook addressBook){
        UpdateWrapper<AddressBook> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",BaseContext.getCurrentId());
        wrapper.set("is_default",0);
        addressBookService.update(wrapper);
        addressBook.setIsDefault(1);
        addressBook.setUpdateTime(addressBook.getUpdateTime());
        //为了设置默认地址后地址列表不会变化
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }
    //根据id查询地址
    @GetMapping("/{id}")
    public R<AddressBook> byid(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        if(byId == null){
            return R.error("没有找到该对象");
        }
        return R.success(byId);
    }
    //删除地址
    @DeleteMapping
    public R<String> delete(@RequestParam Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }
    //修改地址
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        Long id = addressBook.getId();
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
    //查询默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.eq("is_default",1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if(addressBook!=null){
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }
}
